package cn.mymall.web.controller.houtai.mall;

import cn.mymall.entity.mall.Picture;
import cn.mymall.service.mall.IPictureService;
import cn.mymall.support.ResultCode;
import cn.mymall.util.BaseWebUtil;
import cn.mymall.util.BindUtils;
import cn.mymall.web.controller.houtai.BaseHoutaiController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PictureApiController extends BaseHoutaiController {
	@Autowired
	private IPictureService pictureService;

	// 列表
	@RequestMapping("/api/picture/list")
	public ResultCode list(Integer productId) {
		QueryWrapper<Picture> ew = new QueryWrapper<>();
		ew.eq("product_id", productId);
		ew.orderByAsc("sort_num");
		List<Picture> list = pictureService.list(ew);
		Map map = new HashMap<>();
		map.put("pictureList", list);
		return ResultCode.getSuccessReturn(map);
	}

	// 新增or修改
	@RequestMapping("/api/picture/delete")
	public ResultCode modifyCategory(Integer id) {
		pictureService.removeById(id);
		return ResultCode.SUCCESS;
	}

	// 保存
	@RequestMapping("/api/picture/save")
	public ResultCode saveCategory(Integer id) {
		Picture picture = new Picture();
		BindUtils.bindData(picture, BaseWebUtil.getRequestParams());
		pictureService.saveOrUpdate(picture);
		return ResultCode.SUCCESS;
	}
	@RequestMapping("/api/picture/setSortNum")
	public ResultCode modifyCategory(Integer id, Integer sortNum) {
		Picture picture = pictureService.getById(id);
		picture.setSortNum(sortNum);
		pictureService.saveOrUpdate(picture);
		return ResultCode.SUCCESS;
	}
}
